#include <iostream>
#include <cassert>
#include "../src/utils/string.h"
#include "../src/utils/json.h"
#include "../src/asset/model.h"

void testStringUtils() {
    // 测试trim
    assert(StringUtils::trim("  test  ") == "test");
    assert(StringUtils::trim("\t\nhello\r") == "hello");
    assert(StringUtils::trim("") == "");
    
    // 测试toLower和toUpper
    assert(StringUtils::toLower("Hello World") == "hello world");
    assert(StringUtils::toUpper("Hello World") == "HELLO WORLD");
    
    // 测试split
    std::vector<std::string> parts = StringUtils::split("a,b,c", ',');
    assert(parts.size() == 3);
    assert(parts[0] == "a");
    assert(parts[1] == "b");
    assert(parts[2] == "c");
    
    // 测试startsWith和endsWith
    assert(StringUtils::startsWith("hello", "he"));
    assert(!StringUtils::startsWith("hello", "hi"));
    assert(StringUtils::endsWith("hello", "lo"));
    assert(!StringUtils::endsWith("hello", "ly"));
    
    std::cout << "All string utility tests passed!" << std::endl;
}

void testJsonUtils() {
    // 创建一个测试资产
    Asset asset;
    asset.id = 1;
    asset.name = "Test Asset";
    asset.type = "Equipment";
    asset.description = "Test description with \"quotes\"";
    asset.location = "Warehouse A";
    asset.value = 1234.56;
    asset.status = "In Use";
    
    // 序列化资产
    std::string json = JsonUtils::serializeAsset(asset);
    
    // 反序列化资产
    Asset parsedAsset = JsonUtils::parseAsset(json);
    
    // 验证反序列化结果
    assert(parsedAsset.id == asset.id);
    assert(parsedAsset.name == asset.name);
    assert(parsedAsset.type == asset.type);
    assert(parsedAsset.description == asset.description);
    assert(parsedAsset.location == asset.location);
    assert(parsedAsset.value == asset.value);
    assert(parsedAsset.status == asset.status);
    
    // 测试资产列表序列化和反序列化
    std::vector<Asset> assets;
    assets.push_back(asset);
    
    Asset asset2;
    asset2.id = 2;
    asset2.name = "Another Asset";
    asset2.type = "Furniture";
    assets.push_back(asset2);
    
    std::string jsonList = JsonUtils::serializeAssets(assets);
    std::vector<Asset> parsedAssets = JsonUtils::parseAssets(jsonList);
    
    assert(parsedAssets.size() == 2);
    assert(parsedAssets[1].name == "Another Asset");
    
    std::cout << "All JSON utility tests passed!" << std::endl;
}

int main() {
    testStringUtils();
    testJsonUtils();
    return 0;
}
